//
//  KRLLoginViewController.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/12.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLLoginViewController.h"
#import "KRLCountryViewController.h"

@interface KRLLoginViewController ()

@property (weak, nonatomic) IBOutlet UILabel *countryLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaCodeLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *eyeButton;

@end

@implementation KRLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.eyeButton setImage:[UIImage imageNamed:@"icon_hide"] forState:UIControlStateSelected];
}

- (IBAction)didClickLoginButton:(UIButton *)sender {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSDictionary *params = @{
                             @"api_code": @"Login/loginPhone",
                             @"mobile": @"8618826476293",
                             @"password": [@"ngexin1116" md5],
                             };
    [session POST:@"http://api.kara.live/apiv2/Login/loginPhone" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *userToken = responseObject[@"results"][@"user_token"];
        [KRLUserModel.sharedUser updateUserToken:userToken];
        NSLog(@"login success");
        if (self->_loginBlcok) {
            self->_loginBlcok(YES);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"login failure");
        if (self->_loginBlcok) {
            self->_loginBlcok(NO);
        }
    }];
}

- (IBAction)didClickEyeButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.passwordTextField.secureTextEntry = !sender.selected;
}

- (IBAction)didClickCountryButton:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    KRLCountryViewController *controller = [[KRLCountryViewController alloc] init];
    controller.selectBlock = ^(NSString *country, NSString *areaCode) {
        weakSelf.countryLabel.text = country;
        weakSelf.areaCodeLabel.text = [NSString stringWithFormat:@"+%@", areaCode];
    };
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentViewController:nvc animated:YES completion:nil];
}

- (IBAction)didClickBackButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
